<?php
/**
 * Created by Zhy
 * User: Zhy
 * Date: 2018/12/27
 * Time: 10:26
 */

namespace app\api\controller;


use think\Db;

class Banner extends Base
{
    /**
     * 登录绑定
     */
    public function onlylogin(){
        extract(input());
        $user = Db::table('up_factory_member')
            ->where('m_serial', $number)
            ->find();
        if(!$user){
            return json(['status' => 0, 'msg' => '编号错误，请重新输入']);
        }else{
            if($user['m_name'] != $name){
                return json(['status' => 0, 'msg' => '姓名错误，请重新输入']);
            }else{
                if($user['m_machine']){
                    return json(['status' => 0, 'msg' => '此账号已经登录过了']);
                }else{
    
                    $user_machine = Db::table('up_factory_member')
                        ->where('m_machine', $machine)
                        ->select();
                    if($user_machine){
                        foreach($user_machine as $key => $va){
                            Db::table('up_factory_member')
                                ->where('m_id', $va['m_id'])
                                ->update(['m_machine' => null]);
                        }
                    }
                    
                    $res = Db::table('up_factory_member')
                        ->where('m_serial', $number)
                        ->where('m_name', $name)
                        ->update(['m_machine'=>$machine]);
                    if($res){
                        $user['m_machine'] = $machine;
                        return json(['status' => 200, 'msg' => $user]);
                    }else{
                        return json(['status' => 0, 'msg' => '账号关联失败，请重新登录']);
                    }
                }
            }
        }
    }
    
    /**
     * 启动页Banner
     * 按发布时间倒序，只展示一条
     * @return \think\response\Json
     */
    public function startBanner(){
        $params = input('param.');
        $sheng_admin_id = Db::table('up_factory_member')
            ->join('up_factory', 'factory_id = m_factory_id', 'left')
            ->where('m_id', $params['user_id'])
            ->value('f_id');
        $banner = Db::table('up_banner_start')
            ->field('b_name, b_type, b_url, b_mp_url')
            ->where('b_status', 1)
            ->where('b_user', 0)
            ->whereOr('b_user', $sheng_admin_id)
            ->order('b_publish_time desc')
            ->find();
        if(!empty($banner)){
            $banner['b_url'] = http_type() . $banner['b_url'];
            $banner['b_mp_url'] = http_type() . $banner['b_mp_url'];
            return json(['status' => 200, 'msg' => $banner]);
        }else{
            return json(['status' => 200, 'msg' => $banner]);
        }
    }
    
    /**
     * 待机Banner
     * 分别获取总后台4条banner和省后台4条banner，分别一对一、二对二的方式对比，
     * 按时间倒序生成一个新的数组
     * @return \think\response\Json
     */
    public function holdBanner(){
        $params = input('param.');
        //获取用户所在省的管理员ID
        $sheng_admin_id = Db::table('up_factory_member')
            ->join('up_factory', 'factory_id = m_factory_id', 'left')
            ->where('m_id', $params['user_id'])
            ->value('f_id');
        //总后台广告
        $adminBanner = Db::table('up_banner_hold')
            ->field('b_id, b_name, b_type, b_url,b_update_time, b_mp_url')
            ->where('b_user', 0)
//            ->order('b_update_time desc')
            ->select();
        //用户所在省的广告
        $shengBanner = Db::table('up_banner_hold')
            ->field('b_id, b_name, b_type,b_url,b_update_time, b_mp_url')
            ->where('b_user', $sheng_admin_id)
//            ->order('b_update_time desc')
            ->select();

        $holdBanner = [];//定义最终得出的数组
        if($params['user_id'] == -1 || empty($sheng_admin_id)){//-1为用户未登录   未登录或已删除
            $holdBanner = $adminBanner;
        }else{
            foreach($adminBanner as $key => $va){
                if($va['b_url'] == '' && $shengBanner[$key]['b_url'] != ''){
                    $holdBanner[] = $shengBanner[$key];
                }elseif($shengBanner[$key]['b_url'] == '' && $va['b_url'] != ''){
                    $holdBanner[] = $va;
                }elseif($shengBanner[$key]['b_url'] != '' && $va['b_url'] != ''){
                    if(strtotime($va['b_update_time']) > strtotime($shengBanner[$key]['b_update_time'])){
                        $holdBanner[] = $va;
                    }else{
                        $holdBanner[] = $shengBanner[$key];
                    }
                }else{
                    $holdBanner = [];
                }
            }
        }
        if(!empty($holdBanner)){
            foreach($holdBanner as $k => &$v){
                $v['b_url'] = http_type() . $v['b_url'];
                $v['b_mp_url'] = http_type() . $v['b_mp_url'];
            }
            return json(['status' => 200, 'msg' => $holdBanner]);
        }else{
            return json(['status' => 200, 'msg' => []]);
        }
    }
    
}